#!/bin/bash

test_name="rtsp_rtmp_zlm_ffmpeg"

echo -e "输入测试目标路数："
#键盘读取赋bai值du给变量num
read streamCount
#判断num的值zhi是否dao为数字，条件否则直接zhuan退出shu
expr $streamCount + 0 1>/dev/null 2>&1
if [ $? -ne 0 ]
then
echo "${streamCount} is not a number!"
exit 0
fi
#判断变量num的值是否大于0
if [ "$streamCount" -gt 0 ]
then
echo "测试目标路数：${streamCount}"
else
exit 0
fi

Current_PATH=$(cd $(dirname $0) && pwd)
ffmepgBin="${Current_PATH}/../ffmpeg-4.3.1/ffmpeg"
cd ${Current_PATH}
pwd
./stop.sh
sleep 1
./start.sh
sleep 1
killall ffmpeg
sleep 1

ZLMediaKit_URL="127.0.0.1"
ZLMediaKit_secret="035c73f7-bb6b-4889-a715-d9eb2d1925cc"
defaultVhost="__defaultVhost__"

inputScheam="rtsp"
inputHost="192.168.1.162"
inputPort="554"
inputUrl="${inputScheam}://${inputHost}:${inputPort}/live/test"


for((i=0;i<$streamCount;i++))
do
    outputUrl="${outputScheam}://${outputHost}:${outputPort}/live/ch${i}";
    getUrl="http://${ZLMediaKit_URL}/index/api/addStreamProxy?secret=${ZLMediaKit_secret}&vhost=${defaultVhost}&app=live&stream=ch${i}&url=${inputUrl}&enable_rtsp=0&enable_rtmp=1&enable_hls=0&enable_mp4=0&rtp_type=0"
    echo "${getUrl}"
    wget -qO- "${getUrl}"
    sleep 0.2
    echo "-----------------------------------------------------------"
done

inputScheam="rtmp"
inputHost="127.0.0.1"
inputPort="1935"

outputScheam="rtmp"
outputHost="192.168.1.162"
outputPort="1935"

echo "-----------------------------------------------------------"
for((i=0;i<$streamCount;i++))
do
    inputUrl="${inputScheam}://${inputHost}:${inputPort}/live/ch${i}"
    outputUrl="${outputScheam}://${outputHost}:${outputPort}/live/ch${i}";
    echo "${ffmepgBin} -i ${inputUrl} -c:v copy -c:a copy -f flv ${outputUrl} 1>/dev/null 2>/dev/null &"
    ${ffmepgBin} -i ${inputUrl} -c:v copy -c:a copy -f flv ${outputUrl} 1>/dev/null 2>/dev/null &
    # top -b -n 1 | head -n 5
    sleep 0.2
    echo "-----------------------------------------------------------"
done

echo "开始记录使用情况..."
for((i=0;i<100;i++))
do
    ((sec=${i}*10))
    echo "[sec：${sec}][test：${test_name}][stream：${streamCount}]-----------------------------------------------------------"
    # top -b -n 1 | head -n 5
    # echo "-----------------------------------------------------------"
    # top -b -n 1 | grep ffmpeg
    # echo "-----------------------------------------------------------"
    # top -b -n 1 | grep ffmpeg | wc
    # echo "-----------------------------------------------------------"
    # free -h
    # echo "-----------------------------------------------------------"
    uptime
    free -m
    sleep 10
    echo "-----------------------------------------------------------"
done